/**
 * 查询业务逻辑处理实现类
 * @author 郑翎
 * 2015-10-1 11:07:43
 */
package com.cbts.service.info.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.cbts.dao.ApplayReimDao;
import com.cbts.dao.BusinessTravelListDao;
import com.cbts.dao.DepartmentDao;
import com.cbts.dao.ExchangeRecordDao;
import com.cbts.dao.ReportDao;
import com.cbts.dao.UserInfoDao;
import com.cbts.entity.ApplayReim;
import com.cbts.entity.BusinessTravelList;
import com.cbts.entity.CorporationMonthReport;
import com.cbts.entity.CorporationQuarterReport;
import com.cbts.entity.DepartmentMonthReport;
import com.cbts.entity.DepartmentQuarterReport;
import com.cbts.entity.ExchangeRecord;
import com.cbts.entity.UserInfo;

import com.cbts.service.info.SelectService;
import com.cbts.util.Constant;
import com.cbts.util.Page;


public class SelectServiceImpl implements SelectService {

	private UserInfoDao userInfoDao;						//员工表数据处理类
	private ApplayReimDao applayReimDao;					//报销记录表数据处理类
	private BusinessTravelListDao businessTravelListDao;	//出差列表数据处理类
	private ExchangeRecordDao exchangeRecordDao;			//兑换记录表数据处理类
	private ReportDao reportDao;							//报表数据处理类
	protected DepartmentDao departmentDao;

	/**
	 * 	计算总页数，返回Page对象
	 * @param p
	 * @return
	 */
	public Page totalpage(Page p,int currentPages){
		if (p.getTotalPageNum()/Constant.PAGE>1) {
			if (p.getTotalPageNum()%Constant.PAGE>1) {
				p.setTotalPageNum((p.getTotalPageNum()/Constant.PAGE)+1);
			}else {
				p.setTotalPageNum(p.getTotalPageNum()/Constant.PAGE);
			}
		}else {
			p.setTotalPageNum(1);
		}
		p.setPageNo(currentPages);
		return p;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cbts.service.info.SelectService#getuseInfos(java.lang.String, java.lang.String, int)
	 */
	public Page<UserInfo> getuseInfos(String departmentid,String userid,int flag,int page){
		switch (flag) {
		case 0:
			return userInfoDao.findAllByPage(page, Constant.PAGE);
		case 1:
			String wherehql = " and department.departmentId=?";
			String[] values = new String[1];
			values[0] = departmentid;
			Page p = userInfoDao.findByProperty(wherehql, values, Constant.PAGE*(page-1), Constant.PAGE);
			
			return totalpage(p,page);
		case 2:
			String hql = " and userid=?";
			String[] object = new String[1];
			object[0] = userid;
			
			return userInfoDao.findByProperty(hql, object, 0, 1);
		case 3:
			String userhql = " and userid=? and department.departmentId=? ";
			String[] parms = new String[2];
			parms[0] = userid;
			parms[1] = departmentid;
			
			return userInfoDao.findByProperty(userhql, parms, 0, 1);
		default:
			return null;
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.cbts.service.info.SelectService#getApplayReims(java.lang.String[])
	 */
	public Page<ApplayReim> getApplayReims(String[] conditions,int page) {

			String wherehql = "";
			String[] values = {};
		if (conditions!=null) {
			if (conditions.length > 0) {
				if (conditions[0].length() > 0 ) {
					String temp = conditions[0].replaceAll("-", "");
					wherehql += " and substring(travelOrder,1,8)=" + "\'"
							+ temp + "\'";
				}
				if (conditions[1].length() > 0) {
					wherehql += " and userInfo.department.departmentId=" + "\'" + conditions[1]
							+ "\'";
				}
				if (conditions[2].length() > 0) {
					wherehql += " and userInfo.userid=" + "\'" + conditions[2] + "\'";
				}
				if (conditions[3].length() > 0) {
					wherehql += " and businessTravelList.verifierStatus=" + "\'" + conditions[3] + "\'";
				}else if(conditions[3].length() == 0){
					wherehql += " and (businessTravelList.verifierStatus='待报销' " +
							"or businessTravelList.verifierStatus='已报销'" +
							" or businessTravelList.verifierStatus='报销不通过')";
				}
			}
		}
		if (wherehql.equals("")) {//得到全部的状态是"待报销","已报销","报销s不通过"
			wherehql += " and businessTravelList.verifierStatus='待报销' " +
					"or businessTravelList.verifierStatus='已报销'" +
					" or businessTravelList.verifierStatus='报销不通过'";
			 Page p = applayReimDao.findByProperty(wherehql, values, Constant.PAGE*(page-1), Constant.PAGE);
			 return totalpage(p,page);
		} else {
			Page p = applayReimDao.findByProperty(wherehql, values, Constant.PAGE*(page-1), Constant.PAGE);
			return totalpage(p,page);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.cbts.service.info.SelectService#getbBusinessTravels(java.lang.String[])
	 */
	public Page<BusinessTravelList> getbBusinessTravels(String[] conditions,int page){
		
		String wherehql = "";
		String[] values = {};
		if (conditions!=null) {
			if (conditions.length > 0) {
				if (conditions[0].length() > 0) {
					String temp = conditions[0].replaceAll("-", "");
					wherehql += " and id.substring(travelOrder,1,10)=" + "\'"
							+ temp + "\'";
				}
				if (conditions[1].length() > 0) {
					wherehql += " and userInfoByUserid.department.departmentId=" + "\'" + conditions[1]
							+ "\'";
				}
				if (conditions[2].length() > 0) {
					wherehql += " and userInfoByUserid.userid=" +"\'"+ conditions[2]+"\'";
				}
				if (conditions[3].length() > 0) {
					wherehql += " and verifierStatus=" + "\'" + conditions[3] + "\'";
				}
			}
		}
		if (wherehql == "") {
			return businessTravelListDao.findAllByPage(page, Constant.PAGE);
		} else {
			Page p = businessTravelListDao.findByProperty(wherehql, values, Constant.PAGE*(page-1), Constant.PAGE);
			return totalpage(p, page);
		}
	
	}

	/*
	 * (non-Javadoc)
	 * @see com.cbts.service.info.SelectService#getExchangeRecords(java.lang.String[])
	 */
	public Page<ExchangeRecord> getExchangeRecords(String[] conditions,int page) {

		String wherehql = "";
		String[] values = {};
		if (conditions!=null) {
			if (conditions.length > 0) {
				if (conditions[0].length() > 0) {
					String temp = conditions[0].replaceAll("-", "");
					wherehql += " and id.substring(id.exchangeTime,1,10)="
							+"\'"+ temp+"\'";
				}
				if (conditions[1].length() > 0) {
					wherehql += " and userInfo.department.departmentId=" +"\'"+ conditions[1]+"\'";
				}
				if (conditions[2].length() > 0) {
					wherehql += " and userInfo.userid=" +"\'"+ conditions[2]+"\'";
				}
				if (conditions[3].length() > 0) {
					wherehql += " and exchangeStatus=" + "\'" + conditions[3] + "\'";
				}
			}
		}
		
		if (wherehql == "") {
			return exchangeRecordDao.findAllByPage(page, Constant.PAGE);
		} else {
			Page p = exchangeRecordDao.findByProperty(wherehql, values,Constant.PAGE*(page-1), Constant.PAGE);
			return totalpage(p, page);
		}
	}

	public ApplayReimDao getApplayReimDao() {
		return applayReimDao;
	}

	public void setApplayReimDao(ApplayReimDao applayReimDao) {
		this.applayReimDao = applayReimDao;
	}

	public BusinessTravelListDao getBusinessTravelListDao() {
		return businessTravelListDao;
	}

	public void setBusinessTravelListDao(BusinessTravelListDao businessTravelListDao) {
		this.businessTravelListDao = businessTravelListDao;
	}

	public ExchangeRecordDao getExchangeRecordDao() {
		return exchangeRecordDao;
	}

	public void setExchangeRecordDao(ExchangeRecordDao exchangeRecordDao) {
		this.exchangeRecordDao = exchangeRecordDao;
	}

	public ReportDao getReportDao() {
		return reportDao;
	}

	public void setReportDao(ReportDao reportDao) {
		this.reportDao = reportDao;
	}
	public UserInfoDao getUserInfoDao() {
		return userInfoDao;
	}
	public void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}
	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}
	
	
	public Page<CorporationMonthReport> getCoMonthReport(String year,int page) {
		// TODO Auto-generated method stub
		Page p = reportDao.getReport(0, year, null, Constant.PAGE*(page-1), Constant.PAGE);
		return totalpage(p, page);
	}

	
	public Page<CorporationQuarterReport> getCoQuarterReport(String year,int page) {
		
		 Page p = reportDao.getReport(1, year, null, Constant.PAGE*(page-1), Constant.PAGE);
		 return totalpage(p, page);
	}

	
	public Page<DepartmentMonthReport> getDeMonthReport(String deid, String year,int page) {
		
		Page p = reportDao.getReport(0, year, deid, Constant.PAGE*(page-1), Constant.PAGE);
		return totalpage(p, page);
	}

	
	public Page<DepartmentQuarterReport> getDeQuarterReport(String deid,
			String year,int page) {

		Page p = reportDao.getReport(1, year, deid, Constant.PAGE*(page-1), Constant.PAGE);
		return totalpage(p, page);
	}

	public List<String> getYear(int type, String deid) {

		return reportDao.getYear(type, deid);
	}
	
	
	public Page<DepartmentMonthReport> finadAllDepMon(int page){
		Page p = reportDao.finadAll(0, "", Constant.PAGE*(page-1), Constant.PAGE);
		return totalpage(p, page);
	}
	
	public Page<DepartmentQuarterReport> finadAllDepQua(int page){
		Page p = reportDao.finadAll(1, "", Constant.PAGE*(page-1), Constant.PAGE);
		return totalpage(p, page);
	}
	
	public Page<CorporationMonthReport> finadAllCorMon(int page){
		Page p =  reportDao.finadAll(0, null, Constant.PAGE*(page-1), Constant.PAGE);
		return totalpage(p, page);
	}
	
	public Page<CorporationQuarterReport> finadAllCorQua(int page){
		Page p = reportDao.finadAll(1, null, Constant.PAGE*(page-1), Constant.PAGE);
		return totalpage(p, page);
	}

}
